package com.crd.zk;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Start {
    public static void main(String[] args) throws Exception {
        final MyDataSource datasource = MyDataSource.create("linux122");
        System.out.println("初始化数据域完成！");
        testConnection(datasource);

        // 不停等待用户输入，这期间用来等待数据变更
        while (true) {
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
            datasource.changeDatasourceConfig(name);
            testConnection(datasource);
        }
    }

    private static void testConnection(MyDataSource datasource) throws SQLException {
        try (
                Connection connection = datasource.getConnection();
                ResultSet resultSet = connection.prepareStatement("select user()").executeQuery();

        ) {
            while (resultSet.next()) {
                System.out.println("当前登录的用户名:" + resultSet.getString(1));
            }
        }
    }
}
